<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属那个类型下" prop="urlPuid">
        <el-input
          v-model="queryParams.urlPuid"
          placeholder="请输入所属那个类型下"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台uid" prop="platFormUid">
        <el-input
          v-model="queryParams.platFormUid"
          placeholder="请输入平台uid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="(type类型/page页面)" prop="urlType">
        <el-select v-model="queryParams.urlType" placeholder="请选择(type类型/page页面)" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="链接内容" prop="urlValue">
        <el-input
          v-model="queryParams.urlValue"
          placeholder="请输入链接内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态(1有效/0无效/删除-1)" prop="dataStatus">
        <el-select v-model="queryParams.dataStatus" placeholder="请选择状态(1有效/0无效/删除-1)" clearable size="small">
          <el-option
            v-for="dict in dataStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="dataPriority">
        <el-input
          v-model="queryParams.dataPriority"
          placeholder="请输入排序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人ID" prop="createUid">
        <el-input
          v-model="queryParams.createUid"
          placeholder="请输入创建人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人名称" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人ID" prop="updateUid">
        <el-input
          v-model="queryParams.updateUid"
          placeholder="请输入更新人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新人名称" prop="updateName">
        <el-input
          v-model="queryParams.updateName"
          placeholder="请输入更新人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.updateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['platform:templateurl:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="templateurlList"
      row-key="urlUid"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="所属那个类型下" prop="urlPuid" />
      <el-table-column label="平台uid" align="center" prop="platFormUid" />
      <el-table-column label="(type类型/page页面)" align="center" prop="urlType" />
      <el-table-column label="链接内容" align="center" prop="urlValue" />
      <el-table-column label="状态(1有效/0无效/删除-1)" align="center" prop="dataStatus" :formatter="dataStatusFormat" />
      <el-table-column label="排序" align="center" prop="dataPriority" />
      <el-table-column label="创建人ID" align="center" prop="createUid" />
      <el-table-column label="创建人名称" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人ID" align="center" prop="updateUid" />
      <el-table-column label="更新人名称" align="center" prop="updateName" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['platform:templateurl:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['platform:templateurl:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['platform:templateurl:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改平台_模板链接配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属那个类型下" prop="urlPuid">
          <treeselect v-model="form.urlPuid" :options="templateurlOptions" :normalizer="normalizer" placeholder="请选择所属那个类型下" />
        </el-form-item>
        <el-form-item label="平台uid" prop="platFormUid">
          <el-input v-model="form.platFormUid" placeholder="请输入平台uid" />
        </el-form-item>
        <el-form-item label="(type类型/page页面)" prop="urlType">
          <el-select v-model="form.urlType" placeholder="请选择(type类型/page页面)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="链接内容" prop="urlValue">
          <el-input v-model="form.urlValue" placeholder="请输入链接内容" />
        </el-form-item>
        <el-form-item label="状态(1有效/0无效/删除-1)" prop="dataStatus">
          <el-select v-model="form.dataStatus" placeholder="请选择状态(1有效/0无效/删除-1)">
            <el-option
              v-for="dict in dataStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="dataPriority">
          <el-input v-model="form.dataPriority" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="创建人ID" prop="createUid">
          <el-input v-model="form.createUid" placeholder="请输入创建人ID" />
        </el-form-item>
        <el-form-item label="创建人名称" prop="createName">
          <el-input v-model="form.createName" placeholder="请输入创建人名称" />
        </el-form-item>
        <el-form-item label="更新人ID" prop="updateUid">
          <el-input v-model="form.updateUid" placeholder="请输入更新人ID" />
        </el-form-item>
        <el-form-item label="更新人名称" prop="updateName">
          <el-input v-model="form.updateName" placeholder="请输入更新人名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTemplateurl, getTemplateurl, delTemplateurl, addTemplateurl, updateTemplateurl, exportTemplateurl } from "@/api/platform/templateurl";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Templateurl",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 平台_模板链接配置表格数据
      templateurlList: [],
      // 平台_模板链接配置树选项
      templateurlOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态(1有效/0无效/删除-1)字典
      dataStatusOptions: [],
      // 查询参数
      queryParams: {
        urlPuid: null,
        platFormUid: null,
        urlType: null,
        urlValue: null,
        dataStatus: null,
        dataPriority: null,
        createUid: null,
        createName: null,
        createTime: null,
        updateUid: null,
        updateName: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_data_status").then(response => {
      this.dataStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询平台_模板链接配置列表 */
    getList() {
      this.loading = true;
      listTemplateurl(this.queryParams).then(response => {
        this.templateurlList = this.handleTree(response.data, "urlUid", "urlPuid");
        this.loading = false;
      });
    },
    /** 转换平台_模板链接配置数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.urlUid,
        label: node.urlValue,
        children: node.children
      };
    },
	/** 查询平台_模板链接配置下拉树结构 */
    getTreeselect() {
      listTemplateurl().then(response => {
        this.templateurlOptions = [];
        const data = { urlUid: 0, urlValue: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "urlUid", "urlPuid");
        this.templateurlOptions.push(data);
      });
    },
    // 状态(1有效/0无效/删除-1)字典翻译
    dataStatusFormat(row, column) {
      return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        urlUid: null,
        urlPuid: null,
        platFormUid: null,
        urlType: null,
        urlValue: null,
        dataStatus: null,
        dataPriority: null,
        createUid: null,
        createName: null,
        createTime: null,
        updateUid: null,
        updateName: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.urlUid) {
        this.form.urlPuid = row.urlUid;
      } else {
        this.form.urlPuid = 0;
      }
      this.open = true;
      this.title = "添加平台_模板链接配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.urlPuid = row.urlUid;
      }
      getTemplateurl(row.urlUid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改平台_模板链接配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.urlUid != null) {
            updateTemplateurl(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemplateurl(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除平台_模板链接配置编号为"' + row.urlUid + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTemplateurl(row.urlUid);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    }
  }
};
</script>
